package cn.com.xy.sms.sdk.net.util;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;


public class Command {


    private static final String NOWAIT = "-noWait";

    private static final String WAIT_DATE_PRE = "-wait=";

    private static final String TARGET_IDS_PRE = "-ids=";

    private static final String DOMAIN_PRE = "-domain=";

    private static final String SQL = "-sql=";

    /**
     * 命令针对哪个接口 0: 表示所有接口 1: 检测算法更新接口 2: 获取token接口 3: 归属地查询接口 4: 企业号资源检测下载接口
     * 5: 企业号资料获取接口 6: 情景配置获取接口 7: 情景规则（情景方案）更新请求 8: 检测更新情景导向数据 9: 车次查询接口
     * 10:删除情景缓存 11:sql执行
     */
    private int target;

    /**
     * 是否立即执行
     */
    private boolean isNoWait = false;

    /**
     * 重置接口执行周期（天）
     */
    private int resetWaitDatePeriod = -1;

    /**
     * 重置接口访问地址
     */
    private String resetDomainUrl;

    /**
     * sql
     */
    private String sql;

    /**
     * 立即更新的id集合
     */
    private String[] targetIds;

    /**
     * 其它命令动作
     */
    private Map<String, String> others;

    /**
     * 原始命令格式
     */
    private String commandRes;

    /**
     * @param target
     *            命令针对哪个接口 0: 表示所有接口 1: 检测算法更新接口 2: 获取token接口 3: 归属地查询接口
     *            4:企业号资源检测下载接口 5: 企业号资料获取接口 6: 情景配置获取接口 7: 情景规则（情景方案）更新请求
     *            8:检测更新情景导向数据 9: 车次查询接口 10:删除情景缓存 11:sql执行
     * @param commandRes
     *            原始格式
     */
    public Command(int target, String commandRes) {
        this.target = target;
        this.commandRes = commandRes;
        explain();
    }

    protected void explain() {
        String[] cmds = commandRes.split("\\s(?=-[a-zA-Z]+)");
        for (String s : cmds) {
            if (s.equals(NOWAIT)) {
                this.isNoWait = true;
            } else if (s.startsWith(WAIT_DATE_PRE)) {
                this.resetWaitDatePeriod = Integer.valueOf(s.substring(WAIT_DATE_PRE.length()));
            } else if (s.startsWith(TARGET_IDS_PRE)) {
                this.targetIds = s.substring(TARGET_IDS_PRE.length()).split(",");
                this.isNoWait = true;
            } else if (s.startsWith(DOMAIN_PRE)) {
                resetDomainUrl = s.substring(DOMAIN_PRE.length());
            } else if (s.startsWith(SQL)) {
                sql = s.substring(SQL.length());
            } else {
                if (others == null) {
                    others = new HashMap<String, String>();
                }
                String[] cmd = s.split("=");
                if (cmd.length < 2) {
                    others.put(cmd[0], "true");
                } else {
                    others.put(cmd[0], cmd[1]);
                }
            }
        }
    }

    public int getTarget() {
        return target;
    }

    public void setTarget(int target) {
        this.target = target;
    }

    public boolean isNoWait() {
        return isNoWait;
    }

    public void setNoWait(boolean isNoWait) {
        this.isNoWait = isNoWait;
    }

    public int getResetWaitDatePeriod() {
        return resetWaitDatePeriod;
    }

    public void setResetWaitDatePeriod(int resetWaitDatePeriod) {
        this.resetWaitDatePeriod = resetWaitDatePeriod;
    }

    public String getResetDomainUrl() {
        return resetDomainUrl;
    }

    public void setResetDomainUrl(String resetDomainUrl) {
        this.resetDomainUrl = resetDomainUrl;
    }

    public String getSql() {
        return sql;
    }

    public void setSql(String sql) {
        this.sql = sql;
    }

    public String[] getTargetIds() {
        return targetIds;
    }

    public void setTargetIds(String[] targetIds) {
        this.targetIds = targetIds;
    }

    public Map<String, String> getOthers() {
        return others;
    }

    public void setOthers(Map<String, String> others) {
        this.others = others;
    }

    public String getCommandRes() {
        return commandRes;
    }

    public void setCommandRes(String commandRes) {
        this.commandRes = commandRes;
    }

    @Override
    public String toString() {
        return new StringBuffer("cmd : ").append(commandRes).append("\n targetTo interface:").append(target == 0 ? "all" : target)
                .append("\n execute right now? ").append(isNoWait).append("\n just for this ids:")
                .append(targetIds == null ? "all" : Arrays.toString(targetIds)).append("\n reset Wait Date Period to ")
                .append(resetWaitDatePeriod == -1 ? "no change" : resetWaitDatePeriod).append("\n reset Domain Url to ")
                .append(resetDomainUrl == null ? "no change" : resetDomainUrl).append("\n sql:").append(sql == null ? "nosql to execute" : sql)
                .append("\n other cmd:" + others).toString();
    }


}
